﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic; 
using Ccit.Global;
using System.Collections; 
using System.Text;
using Ccit.OA.MsNotify;
using System.Xml;
using Ccit.OA.WorkRemind;
using Ccit.SysMsgRemind;
namespace Ccit.OA.Notify
{
    public class NotifySQL:INotify
    {
        #region 获取菜单树信息
        /// <summary>
        /// 获取菜单树信息
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        public string GetNotiFyNodes(string Query)
        {
            string cmdText = "select NTF_A1_10,NTF_A1_20,NTF_A1_60 from NTF_A1 where " + Query + "";
            StringBuilder sbXML = new StringBuilder("<ADDRS>"); 
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    string title = "";
                    if (iReader["NTF_A1_60"].ToString() == "0")
                    {
                        title = iReader["NTF_A1_20"].ToString();
                    }
                    else
                    {
                        title = System.Web.HttpContext.Current.Server.HtmlDecode("<font color='red'>"+iReader["NTF_A1_20"].ToString()+"</font>");
                    }
                    sbXML.Append("<ADDR><NotiID>").Append(iReader["NTF_A1_10"].ToString()).Append("</NotiID>");
                    sbXML.Append("<NotiNAME><![CDATA[").Append(title).Append("]]></NotiNAME>");
                    sbXML.Append("</ADDR>");
                }
            }
            sbXML.Append("</ADDRS>");
            return sbXML.ToString();
        }

        #endregion

        #region 保存通知类型
        /// <summary>
        /// 保存通知类型
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveNotiFy(MsNotifyClass ms)
        {
            string cmdText = "";
            List<string> lstResult = new List<string>();
            if(ms.id=="")
            {
                string curYear = DateTime.Now.ToString("yyyy-MM-dd");
                LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
                ms.id = DataInterface.GetMaxId("NTF_A1", "NTF_A1_10").ToString();
                cmdText = "insert into NTF_A1 values(" + ms.id + ",'" + ms.name.Replace("'", "''") + "','" + ms.rangemark + "',"+
                    "'" + ms.range + "','"+ms.temcontent.Replace("'","''")+"',"+ms.state+","+
                    "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
            }
            else
            {
                cmdText = "update NTF_A1 set NTF_A1_20='" + ms.name.Replace("'", "''") + "',NTF_A1_30='" + ms.rangemark + "',"+
                    "NTF_A1_40='" + ms.range + "',NTF_A1_50='" + ms.temcontent.Replace("'", "''") + "',NTF_A1_60='" + ms.state + "' " +
                    " where NTF_A1_10="+ms.id;
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if(iNum>0)
            {
                lstResult.Add(ms.id);
                lstResult.Add(ms.name);
                lstResult.Add("保存成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("");
                lstResult.Add("服务器忙,请稍候重新操作!");
            }
            return lstResult;
        }

        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="sId"></param>
        /// <returns></returns>
        public List<string> DoOperateNotiFy(string sId)
        {
            string cmdText = "delete from NTF_A1 where NTF_A1_10="+sId;
            List<string> lstResult = new List<string>();
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(sId); 
                lstResult.Add("操作成功");
            }
            else
            {
                lstResult.Add("-999"); 
                lstResult.Add("服务器忙,请稍候重新操作!");
            }
            return lstResult;
        }

        #endregion

        #region 获取类型信息
        /// <summary>
        /// 获取类型信息
        /// </summary>
        /// <param name="sId"></param>
        /// <returns></returns>
        public MsNotifyClass GetNotiFyMes(string sId)
        {
            string cmdText = "select NTF_A1_10, NTF_A1_20,NTF_A1_30,NTF_A1_40,NTF_A1_50,NTF_A1_60 from NTF_A1 where NTF_A1_10=" + sId;
            MsNotifyClass ms = null;
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    ms = new MsNotifyClass(iReader["NTF_A1_10"].ToString(), iReader["NTF_A1_20"].ToString(), iReader["NTF_A1_30"].ToString(),
                        iReader["NTF_A1_40"].ToString(), iReader["NTF_A1_50"].ToString(), iReader["NTF_A1_60"].ToString());
                }
            }
            return ms;
        } 
        #endregion

        #region 获取通知发布列表信息
        /// <summary>
        /// 获取通知发布列表信息
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        public List<MsNotifyMessage> GetNotifyMessage(string Query)
        {  
            string sWelCalse = "";
            if (Query != "")
            {
                sWelCalse = GetReTurnQuery(Query);
            }
            string cmdText = "select NTF_A2_10,NTF_A2_20,NTF_A1_20,NTF_A2_30,bas_a1_40, deptname,NTF_A2_9995,NTF_A2_90";
            cmdText += ",NTF_A2_70,NTF_A2_80,NTF_A2_9990,NTF_A1_30,NTF_A1_40,NTF_A1_9990 from NTF_A2 ";
            cmdText += " left join bas_a1 on bas_a1_30 = NTF_A2_100  left join v_dept on deptID = NTF_A2_50 ";
            cmdText += " left join NTF_A1 on NTF_A1_10 = NTF_A2_30   where 1=1   " + sWelCalse + " order by NTF_A2_9995 desc";
            List<MsNotifyMessage> lstNotify = new List<MsNotifyMessage>();
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"]; 
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while(iReader.Read())
                {
                    string YDCount = GetNotifyCount(iReader["NTF_A2_10"].ToString());
                    string iCount = SysInterface.GetUserIdsByRangeType(iReader["NTF_A2_70"].ToString(),
                              iReader["NTF_A2_80"].ToString(), iReader["NTF_A2_9990"].ToString());
                    string[] replyCount = iCount.Split(',');
                    string UserCount = SysInterface.GetUserIdsByRangeType(iReader["NTF_A1_30"].ToString(),
                                                  iReader["NTF_A1_40"].ToString(), iReader["NTF_A1_9990"].ToString());
                    string[] flowid = UserCount.Split(',');
                    bool flag = false;
                        for (int i = 0; i < flowid.Length; i++)
                        {
                            if (flowid[i] == login.UserId)
                            {
                                flag = true;
                                break;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        if (flag == true)
                        {
                            string iCount1 = YDCount + "/" + replyCount.Length;
                            MsNotifyMessage ms = new MsNotifyMessage(iReader["NTF_A2_10"].ToString(), iReader["NTF_A2_20"].ToString(),
                                iReader["NTF_A1_20"].ToString(), null, iReader["deptname"].ToString(),
                                null, null, null,
                                null, iReader["NTF_A2_90"].ToString(), iReader["bas_a1_40"].ToString(),
                                iReader["NTF_A2_9995"].ToString(), iCount1,null,null,null,null);
                            lstNotify.Add(ms);
                        }
                }
            }
            return lstNotify;
        }
        private string GetNotifyCount(string NotifyId)
        {
            string Count = "0";
            string cmdText = "select count(*) as iCount from NTF_A3 where NTF_A3_10="+NotifyId;
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    Count = iReader["iCount"].ToString();
                }
            }
            return Count;
        }
        #endregion

        #region 删除通知信息和浏览信息
        /// <summary>
        /// 删除通知信息和浏览信息
        /// </summary>
        /// <param name="NotifyId">主键ID</param>
        /// <param name="type">判断类型1:删除，0:修改</param>
        /// <param name="state">修改参数</param>
        /// <returns></returns>
        public List<string> DoOperateNotify(string NotifyId, string type, string state)
        {
            string cmdText = "";
            List<string> lstResult = new List<string>();
            List<string> lstCmdText = new List<string>();
            List<string> lstCount = GetState(NotifyId);
            string iCount = "";
            string NId = ""; 
            string[] iCountState=null;
            WorkRemindBLL Bll = new WorkRemindBLL();
            string[] NID = null;
            if (type != "3")
            {
                if (lstCount.Count > 0)
                {
                    if (lstCount[0] != "")
                    {
                        if (lstCount[0].Length > 1)
                        {
                            iCountState = lstCount[0].Split(',');
                            NID = lstCount[1].Split(',');
                        }
                        else
                        {
                            iCount = lstCount[0].ToString();
                            NId = lstCount[1].ToString();
                        }
                    } 
                }
                else
                {
                    if (type == "1")
                    {
                        lstResult.Add("-999");
                        lstResult.Add("通知已发布,不允许删除!");
                    }
                    else
                    {
                        lstResult.Add("-999");
                        lstResult.Add("通知已发布!");
                    }
                    return lstResult;
                }
                if (iCount == "")
                {
                    for (int i = 0; i < iCountState.Length; i++)
                    {
                        for (int j = i; j < NID.Length; j++)
                        {
                            if (type == "1")
                            {
                                cmdText = " delete NTF_A2  where NTF_A2_10 in(" + NID[j] + ")";
                                lstCmdText.Add(cmdText);
                                cmdText = " delete NTF_A3 where NTF_A3_10 = " + NID[j] + " ";
                                lstCmdText.Add(cmdText);
                                cmdText = Bll.DoOperateFileReturn("-1", NID[j]);
                                lstCmdText.Add(cmdText);
                                break;
                            }
                            else
                            {
                                if (iCountState[i].ToString() == "0")
                                {
                                    state = "1";
                                }
                                else
                                {
                                    state = "0";
                                    MsNotifyMessage ms = GetNotiFyMessage(NID[j]);
                                    /// xml格式
                                    /// <RemParam>
                                    ///        <Mode>
                                    ///            <FuncId>功能ID</FuncId>
                                    ///            <ResId>资源ID</ResId>
                                    ///            <RecType>接收人类型</RecType>
                                    ///            <RecId>接收人id串</RecId>
                                    ///        </Mode>           
                                    /// </RemParam>                 
                                    StringBuilder sbXML = new StringBuilder();
                                    sbXML.Append("<RemParam>");
                                    sbXML.Append("<Mode><FuncId>-1</FuncId>");
                                    sbXML.Append("<ResId>" + NID[j] + "</ResId>");
                                    sbXML.Append("<RecType>'" + ms.rangemark + "'</RecType>");
                                    sbXML.Append("<RecId><![CDATA[").Append("" + ms.range + "").Append("]]></RecId>");
                                    sbXML.Append("</Mode>");
                                    sbXML.Append("</RemParam>");
                                    string sResult = Bll.SendRemind(sbXML.ToString());
                                    sbXML = null;
                                    //Bll = null; 
                                }
                                cmdText = " update NTF_A2 set NTF_A2_90=" + state + "  where NTF_A2_10 in(" + NID[j] + ")";
                                lstCmdText.Add(cmdText);
                                break;
                            }                           
                        }
                    }
                    Bll = null;
                }
                else
                {
                    if (type == "1")
                    {
                        cmdText = "delete NTF_A2  where NTF_A2_10 in(" + NId + ")";
                        lstCmdText.Add(cmdText);
                        cmdText = " delete NTF_A3 where NTF_A3_10 = " + NId + " ";
                        lstCmdText.Add(cmdText);
                        cmdText = Bll.DoOperateFileReturn("-1", NId);
                        lstCmdText.Add(cmdText);
                        Bll = null;
                    }
                    else
                    {
                        if (iCount == "0")
                        {
                            state = "1";
                        }
                        else
                        {
                            state = "0";
                            MsNotifyMessage ms = GetNotiFyMessage(NId);
                            /// xml格式
                            /// <RemParam>
                            ///        <Mode>
                            ///            <FuncId>功能ID</FuncId>
                            ///            <ResId>资源ID</ResId>
                            ///            <RecType>接收人类型</RecType>
                            ///            <RecId>接收人id串</RecId>
                            ///        </Mode>           
                            /// </RemParam>               
                            StringBuilder sbXML = new StringBuilder();
                            sbXML.Append("<RemParam>");
                            sbXML.Append("<Mode><FuncId>-1</FuncId>");
                            sbXML.Append("<ResId>" + NId + "</ResId>");
                            sbXML.Append("<RecType>'" + ms.rangemark + "'</RecType>");
                            sbXML.Append("<RecId><![CDATA[").Append("" + ms.range + "").Append("]]></RecId>");
                            sbXML.Append("</Mode>");
                            sbXML.Append("</RemParam>");
                            string sResult = Bll.SendRemind(sbXML.ToString());
                            sbXML = null;
                            Bll = null; 
                        }
                        cmdText = "update NTF_A2 set NTF_A2_90=" + state + "  where NTF_A2_10 in(" + NId + ")";
                        lstCmdText.Add(cmdText);                       
                    }
                }
            }
            else
            {
                if (state == "0")
                {
                    state = "1";
                    cmdText = "delete NTF_A3 where NTF_A3_10="+NotifyId;
                    lstCmdText.Add(cmdText);
                }
                else
                {
                    state = "0";
                    MsNotifyMessage ms = GetNotiFyMessage(NotifyId);
                    /// xml格式
                    /// <RemParam>
                    ///        <Mode>
                    ///            <FuncId>功能ID</FuncId>
                    ///            <ResId>资源ID</ResId>
                    ///            <RecType>接收人类型</RecType>
                    ///            <RecId>接收人id串</RecId>
                    ///        </Mode>           
                    /// </RemParam>         
                    StringBuilder sbXML = new StringBuilder();
                    sbXML.Append("<RemParam>");
                    sbXML.Append("<Mode><FuncId>-1</FuncId>");
                    sbXML.Append("<ResId>" + NotifyId + "</ResId>");
                    sbXML.Append("<RecType>'" + ms.rangemark + "'</RecType>");
                    sbXML.Append("<RecId><![CDATA[").Append("" + ms.range + "").Append("]]></RecId>");
                    sbXML.Append("</Mode>");
                    sbXML.Append("</RemParam>");
                    string sResult = Bll.SendRemind(sbXML.ToString());
                    sbXML = null;
                    Bll = null; 
                }
                cmdText = " update NTF_A2 set NTF_A2_90=" + state + "  where NTF_A2_10 in(" + NotifyId + ")";
                lstCmdText.Add(cmdText);
            }
            int iNum = DataAccessor.ExecuteNonQuery(lstCmdText);
            if(iNum>0)
            {
                lstResult.Add(NotifyId);
                lstResult.Add("操作成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("服务器忙,请稍候重新操作!");
            }
            return lstResult;
        }
        private List<string> GetState(string NotifyId)
        {
            string cmdText = "select NTF_A2_10,NTF_A2_90 from NTF_A2 where NTF_A2_10 in ("+NotifyId+")";
            string state = "";
            string NId = "";
            List<string> lstResult = new List<string>();
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while(iReader.Read())
                {
                    if (iReader["NTF_A2_90"].ToString() == "1")
                    {
                        state += (state == "") ? iReader["NTF_A2_90"].ToString() : "," + iReader["NTF_A2_90"].ToString();
                        NId += (NId == "") ? iReader["NTF_A2_10"].ToString() : "," + iReader["NTF_A2_10"].ToString();
                    }
                }
                if (state != "")
                {
                    lstResult.Add(state);
                    lstResult.Add(NId);
                }
            }
            return lstResult;
        }
        #endregion

        #region 添加通知类型
        /// <summary>
        /// 添加通知类型
        /// </summary>
        /// <param name="ddlList"></param>
        public void DoAddDownList(ref DropDownList ddlList)
        {
            ddlList.Items.Clear();
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "select NTF_A1_10,NTF_A1_20,NTF_A1_30,NTF_A1_40,NTF_A1_9990 from NTF_A1 where NTF_A1_60=0";
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while(iReader.Read())
                {
                   string UserCount = SysInterface.GetUserIdsByRangeType(iReader["NTF_A1_30"].ToString(),
                                                  iReader["NTF_A1_40"].ToString(), iReader["NTF_A1_9990"].ToString());
                    string[] flowid = UserCount.Split(',');
                    bool flag = false;
                        for (int i = 0; i < flowid.Length; i++)
                        {
                            if (flowid[i] == login.UserId)
                            {
                                flag = true;
                                break;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        if (flag == true)
                        {
                            ddlList.Items.Add(new ListItem(iReader["NTF_A1_20"].ToString(), iReader["NTF_A1_10"].ToString()));
                        }
                }
            }
        }

        #endregion

        #region 保存NTF_A2(通知信息表)
        /// <summary>
        /// 保存NTF_A2(通知信息表)
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveNotiFyMessage(MsNotifyMessage ms)
        {
            List<string> lstResult = new List<string>();
            string cmdText = "";
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            if(ms.id=="")
            {
                string curYear = DateTime.Now.ToString("yyyy-MM-dd"); 
                ms.id = DataInterface.GetMaxId("NTF_A2", "NTF_A2_10").ToString();
                cmdText = "insert into NTF_A2(NTF_A2_10,NTF_A2_20,NTF_A2_30,NTF_A2_40,NTF_A2_50,NTF_A2_60,NTF_A2_70,"+
                          "NTF_A2_80,NTF_A2_90,NTF_A2_100,NTF_A2_110,NTF_A2_9990,NTF_A2_9991,NTF_A2_9992,NTF_A2_9993,"+
                          "NTF_A2_9994,NTF_A2_9995) values(" + ms.id + ",'" + ms.title.Replace("'", "''") + "'," + ms.type + "," +
                          "'"+ms.content.Replace("'","''")+"','"+ms.deptid+"','"+ms.receipt+"','"+ms.rangemark+"','"+ms.range+"',"+
                          ""+ms.state+",'"+ms.importance+"','"+ms.receiptcontent.Replace("'","''")+"',"+
                          "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
            }
            else
            {
                cmdText = "update NTF_A2 set NTF_A2_20='" + ms.title.Replace("'", "''") + "',NTF_A2_30='" + ms.type + "',"+
                          "NTF_A2_40='" + ms.content.Replace("'", "''") + "',NTF_A2_50='" + ms.deptid + "',NTF_A2_60='"+ms.receipt+"',"+
                          "NTF_A2_70='" + ms.rangemark + "',NTF_A2_80='" + ms.range + "',NTF_A2_90=" + ms.state + " ,"+
                          "NTF_A2_100='" + ms.importance + "' ,NTF_A2_110='" + ms.receiptcontent.Replace("'", "''") + "'  where NTF_A2_10=" + ms.id;
            }
            /// xml格式
            /// <RemParam>
            ///        <Mode>
            ///            <FuncId>功能ID</FuncId>
            ///            <ResId>资源ID</ResId>
            ///            <RecType>接收人类型</RecType>
            ///            <RecId>接收人id串</RecId>
            ///        </Mode>           
            /// </RemParam>
            if(ms.state=="0")
            {
                WorkRemindBLL Bll = new WorkRemindBLL();
                StringBuilder sbXML = new StringBuilder();
                sbXML.Append("<RemParam>");
                sbXML.Append("<Mode><FuncId>-1</FuncId>");
                sbXML.Append("<ResId>"+ms.id+"</ResId>");
                sbXML.Append("<RecType>" + ms.rangemark +"</RecType>");
                sbXML.Append("<RecId><![CDATA[").Append("" + ms.range + "").Append("]]></RecId>");
                sbXML.Append("</Mode>");
                sbXML.Append("</RemParam>");                 
                string sResult = Bll.SendRemind(sbXML.ToString());
                sbXML = null;
                Bll = null;
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if(iNum>0)
            {
                lstResult.Add(ms.id);
                lstResult.Add("操作成功!");                
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("服务器忙,请稍候继续操作!");
            }
            return lstResult;
        }

        #endregion

        #region 获取通知内容信息
        /// <summary>
        /// 获取通知内容信息
        /// </summary>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public string GetNotyfyContent(string NotifyId)
        {
            string content = "";
            string cmdText = "select NTF_A1_50 from NTF_A1 where NTF_A1_10="+NotifyId;
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    content = iReader["NTF_A1_50"].ToString();
                }
            }
            return content;
        }

        #endregion

        #region 获取通知类型编剧页面信息
        /// <summary>
        /// 获取通知类型编剧页面信息
        /// </summary>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public MsNotifyMessage GetNotiFyMessage(string NotifyId)
        {
            string cmdtText = "select NTF_A2_10,NTF_A2_20,NTF_A2_30,NTF_A2_40,NTF_A2_50,NTF_A2_60,NTF_A2_70,NTF_A2_80 ";
            cmdtText += ",NTF_A2_90,NTF_A2_100,NTF_A2_110 from NTF_A2";
            cmdtText += "  where NTF_A2_10=" + NotifyId + "";
            MsNotifyMessage ms = null;
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdtText))
            {
                if (iReader.Read())
                {
                    ms = new MsNotifyMessage(iReader["NTF_A2_10"].ToString(), iReader["NTF_A2_20"].ToString(), iReader["NTF_A2_30"].ToString()
                                            , iReader["NTF_A2_40"].ToString(), null, iReader["NTF_A2_50"].ToString(), iReader["NTF_A2_60"].ToString()
                                            , iReader["NTF_A2_70"].ToString(), iReader["NTF_A2_80"].ToString(), iReader["NTF_A2_90"].ToString(),
                                            iReader["NTF_A2_100"].ToString(), null, null, iReader["NTF_A2_110"].ToString(),null,null,null);
                }
            }
            return ms;
        }

        #endregion

        #region 获取回复列表页面
        /// <summary>
        ///  获取回复列表页面
        /// </summary>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public List<MsNotifyMessage> GetReplyViewMes(string NotifyId,string Query)
        {
            
            string cmdText = "select deptname,NTF_A2_60,NTF_A2_70,NTF_A2_80,NTF_A2_9990 from NTF_A2 left join v_dept on deptID = NTF_A2_50 ";
            cmdText += " where NTF_A2_10=" + NotifyId + "";           
            List<MsNotifyMessage> lstMs = new List<MsNotifyMessage>();
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    string UserCount = SysInterface.GetUserIdsByRangeType(iReader["NTF_A2_70"].ToString(),
                                                 iReader["NTF_A2_80"].ToString(), iReader["NTF_A2_9990"].ToString());
                    if (UserCount != "")
                    {
                        string[] flowid = UserCount.Split(',');
                        for (int i = 0; i < flowid.Length; i++)
                        {
                            string state = "1";
                            string iCount = GetUserMessage(flowid[i].ToString(), NotifyId);
                            string[] lstCount = new string[3];
                            lstCount[0] = "";
                            lstCount[1] = "";
                            lstCount[2] = "";
                            if (iCount != "")
                            {
                                lstCount = iCount.Split(',');
                                if (lstCount[0].ToString() != "")
                                {
                                    state = "0";
                                }
                                else
                                { state = "1"; }
                            }
                            string userName = GetUserName(flowid[i].ToString(), Query);
                            string[] lstUser = new string[2];
                            lstUser[0] = "";
                            lstUser[1] = "";
                            if (userName != "")
                            {
                                lstUser = userName.Split(',');
                                MsNotifyMessage ms = new MsNotifyMessage(null, null, null, lstCount[2].ToString(), lstUser[1].ToString(), null, iReader["NTF_A2_60"].ToString(), null, null, state, null
                                                                    , lstCount[1].ToString(), null, null, lstUser[0].ToString(), lstCount[0].ToString(), flowid[i].ToString());
                                lstMs.Add(ms);
                            }
                        }
                    }
                } 
            }
            return lstMs;
        }      
        /// <summary>
        /// 获取浏览信息内容
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public string GetUserMessage(string userId, string NotifyId)
        {
            string cmdText = "select NTF_A3_30,NTF_A3_40,NTF_A3_9995 from NTF_A3 where NTF_A3_10=" + NotifyId + " and NTF_A3_20=" + userId;
            string iNum = "";
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    iNum = iReader["NTF_A3_40"].ToString() + "," + iReader["NTF_A3_9995"].ToString() + "," + iReader["NTF_A3_30"].ToString();
                }
            }
            return iNum;
        }
        private string GetUserName(string userId,string Query)
        {
             string sWelCalse = "";
             if (Query.Trim() != "")
             {
                 if (Query == "1")
                 {
                     sWelCalse += "  and not exists (select ntf_a3_10 from ntf_a3 where ntf_a3_20=userId and ntf_a3_20='" + userId + "')";
                 }
                 if (Query == "2")
                 {
                     sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_20=userId and ntf_a3_20='" + userId + "' and ntf_a3_30 ='')";
                 }
                 if (Query == "3")
                 {
                     sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_20=userId and ntf_a3_20='" + userId + "' and ntf_a3_30 <>'')";
                 }
                 if (Query == "4")
                 {
                     sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_20=userId and ntf_a3_20='" + userId + "')";
                 }
             }
            string cmdText = "select username,deptname from v_udp where userid=" + userId+" "+sWelCalse+"";
            string userName = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    userName = iReader["username"].ToString() + "," + iReader["deptname"].ToString(); ;
                }
            }
            return userName;
        }
        #endregion

        #region 获取条件设置信息
        /// <summary>
        /// 获取条件设置信息
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public DataSet GetQueryMessage(string typeId)
        {
            string cmdText = "select NTF_A4_10 as queryid,NTF_A4_40 as queryname from NTF_A4 where NTF_A4_30=" + typeId;
            DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            return dSet;
        }

        #endregion

        #region 删除条件信息
        /// <summary>
        /// 删除条件信息
        /// </summary>
        /// <param name="queryId"></param>
        /// <returns></returns>
        public string DoOperateQuery(string queryId)
        {
            string cmdText = "delete NTF_A4 where NTF_A4_10="+queryId;
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if(iNum>0)
            { return "1"; }
            else
            { return "-999"; }
        }

        #endregion

        #region 保存条件信息
        /// <summary>
        /// 保存条件信息
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveQuery(MsQueryClass ms)
        {
            string cmdText = "";
            string curYear = DateTime.Now.ToString("yyyy-MM-dd");
            List<string> lstResult = new List<string>();
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            if(ms.queryid=="")
            {
                ms.queryid = DataInterface.GetMaxId("NTF_A4", "NTF_A4_10").ToString();
                cmdText = "insert into NTF_A4(NTF_A4_10,NTF_A4_20,NTF_A4_30,NTF_A4_40,NTF_A4_50,NTF_A4_9990,NTF_A4_9991,"+
                    "NTF_A4_9992,NTF_A4_9993,NTF_A4_9994,NTF_A4_9995)" +
                    " values("+ms.queryid+","+loginUser.UserId+","+ms.typeid+",'"+ms.queryname.Replace("'","''")+"',"+
                    "'"+ms.content.Replace("'","''")+"',"+
                     "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
            }
            else
            {
                cmdText = "update NTF_A4 set NTF_A4_30=" + ms.typeid + ",NTF_A4_40='" + ms.queryname.Replace("'", "''") + "',NTF_A4_50='" + ms.content.Replace("'", "''") + "'"+
                    " where NTF_A4_10="+ms.queryid;
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if(iNum>0)
            {
                lstResult.Add(ms.queryid);
                lstResult.Add("操作成功!");
            }
            else
            {

                lstResult.Add("-999");
                lstResult.Add("服务器忙,请稍候重新操作!");
            }
            return lstResult;
        }

        #endregion

        #region 获取条件信息
        /// <summary>
        /// 获取条件信息
        /// </summary>
        /// <param name="QueryId"></param>
        /// <returns></returns>
        public MsQueryClass GetQueryEdit(string QueryId)
        {
            string cmdText = "select NTF_A4_20,NTF_A4_30,NTF_A4_40,NTF_A4_50 from NTF_A4 where NTF_A4_10="+QueryId;
            MsQueryClass ms = null;
            using(IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    ms = new MsQueryClass(null, null, null, iReader["NTF_A4_40"].ToString(), iReader["NTF_A4_50"].ToString());
                }
            }
            return ms;
        }

        #endregion

        #region INotify 成员
        /// <summary>
        /// 获取类型信息
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        public DataSet GetDictLibMes()
        {
            string cmdText = "select NTF_A1_10 as typeNum,NTF_A1_20 as typeName from NTF_A1 where NTF_A1_60=0";
            DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            return dSet;
        }

        #endregion

        #region 获取通知浏览页面
        /// <summary>
        ///  获取通知浏览页面
        /// </summary>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public List<MsNotifyMessage> GetViewMes(string Query)
        {
            string sWelCalse = "";
            if (Query != "")
            {
                sWelCalse = GetReTurnQuery(Query);
            }
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "select NTF_A2_10,NTF_A2_20,NTF_A1_20,NTF_A2_30,bas_a1_40, deptname,NTF_A2_9995,NTF_A2_90";
            cmdText += ",NTF_A2_60,NTF_A2_70,NTF_A2_80,NTF_A2_9990 from NTF_A2 ";
            cmdText += " left join bas_a1 on bas_a1_30 = NTF_A2_100  left join v_dept on deptID = NTF_A2_50 ";
            cmdText += " left join NTF_A1 on NTF_A1_10 = NTF_A2_30   where NTF_A2_90=0 " + sWelCalse + "  order by NTF_A2_9995 desc";
            List<MsNotifyMessage> lstMs = new List<MsNotifyMessage>();
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while(iReader.Read())
                {
                    string UserCount = SysInterface.GetUserIdsByRangeType(iReader["NTF_A2_70"].ToString(),
                                                 iReader["NTF_A2_80"].ToString(), iReader["NTF_A2_9990"].ToString());
                    string[] flowid = UserCount.Split(',');
                    for (int i = 0; i < flowid.Length; i++)
                    {
                        if (flowid[i] == loginUser.UserId)
                        {
                            string state = "";
                            string iCount = GetUserMessage(flowid[i].ToString(), iReader["NTF_A2_10"].ToString());
                            string[] lstCount = new string[3];
                            lstCount[0] = "";
                            lstCount[1] = "";
                            lstCount[2] = "";
                            if (iCount != "")
                            {
                                lstCount = iCount.Split(',');
                                if (lstCount[0].ToString() != "")
                                {
                                    state = "0";
                                }
                                else
                                { state = "1"; }
                            }
                            string userName = GetUserName(flowid[i].ToString(),"");
                            string[] lstUser = userName.Split(',');
                            MsNotifyMessage ms = new MsNotifyMessage(iReader["NTF_A2_10"].ToString(), iReader["NTF_A2_20"].ToString(), iReader["NTF_A1_20"].ToString(),
                                lstCount[2].ToString(), iReader["deptname"].ToString(), null, iReader["NTF_A2_60"].ToString(), null, null, state, iReader["bas_a1_40"].ToString()
                               , iReader["NTF_A2_9995"].ToString(), null, null, lstUser[0].ToString(), lstCount[0].ToString(), flowid[i].ToString());
                            lstMs.Add(ms);
                        }
                    }
                }
            }
            return lstMs;
        }
        private string GetReTurnQuery(string Query)
        {
            XmlDocument oDoc = new XmlDocument();
            string curYear = DateTime.Now.ToString("yyyy-MM-dd");
            string sWelCalse = "";
            if (Query != "")
            {
                oDoc.LoadXml(Query);
                string TitleName = oDoc.GetElementsByTagName("TitleName")[0].InnerText;
                if (TitleName.Trim() != "")
                {
                    sWelCalse = " and NTF_A2_20 like '%" + TitleName + "%'";
                }
                string Content = oDoc.GetElementsByTagName("Content")[0].InnerText;
                if (Content.Trim() != "")
                {
                    sWelCalse += " and NTF_A2_40 like '%" + Content + "%'";
                }
                string Creator = oDoc.GetElementsByTagName("Creator")[0].InnerText;
                if (Creator.Trim() != "")
                {
                    sWelCalse += " and NTF_A2_9994 in(" + Creator + ")";
                }
                string Level = oDoc.GetElementsByTagName("Level")[0].InnerText;
                if (Level.Trim() != "")
                {
                    sWelCalse += " and NTF_A2_100 ='" + Level + "'";
                }
                string PubFlag = oDoc.GetElementsByTagName("PubFlag")[0].InnerText;
                if (PubFlag.Trim() != "")
                {
                    sWelCalse += " and NTF_A2_90 =" + PubFlag + "";
                }
                string ReadFlag = oDoc.GetElementsByTagName("ReadFlag")[0].InnerText;
                if (ReadFlag.Trim() != "")
                {
                    LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
                    if (ReadFlag == "1")
                    {
                        sWelCalse += "  and not exists (select ntf_a3_10 from ntf_a3 where ntf_a3_10=ntf_A2_10 and ntf_a3_20='"+loginUser.UserId+"')";
                    }
                    if(ReadFlag == "2")
                    {
                        sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_10=ntf_A2_10 and ntf_a3_20='" + loginUser.UserId + "' and ntf_a3_30 ='')";
                    }
                    if (ReadFlag == "3")
                    {
                        sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_10=ntf_A2_10 and ntf_a3_20='" + loginUser.UserId + "' and ntf_a3_30 <>'')";
                    }
                    if (ReadFlag == "4")
                    {
                        sWelCalse += "  and  exists (select ntf_a3_10 from ntf_a3 where ntf_a3_10=ntf_A2_10 and ntf_a3_20='" + loginUser.UserId + "')";
                    }  
                }
                string Class = oDoc.GetElementsByTagName("Class")[0].InnerText;
                if (Class.Trim() != "")
                {
                    sWelCalse += " and NTF_A2_30 in(" + Class + ")";
                }
                XmlNode xmlNode = oDoc.SelectSingleNode("Query/Time");
                string typeCount = xmlNode.Attributes["Type"].Value;
                if (typeCount == "1")
                {
                    sWelCalse += " and NTF_A2_9995 ='" + curYear + "'";
                }
                DateLib dateLib = new DateLib(curYear);
                if (typeCount == "2")
                {
                    //本周第一天和最后一天
                    sWelCalse += " and NTF_A2_9995 >='" + dateLib.GetFirstDateOfWeek() + "' and NTF_A2_9995 <='" + dateLib.GetLastDateOfWeek() + "'";
                }
                if (typeCount == "3")
                {
                    //本月第一天和最后一天              
                    sWelCalse += " and NTF_A2_9995 >='" + dateLib.GetFirstDateOfMonth() + "' and NTF_A2_9995 <='" + dateLib.GetEndDateOfMonth() + "'";
                }
                if (typeCount == "4")
                {
                    int dayCount = int.Parse(oDoc.GetElementsByTagName("Time")[0].InnerText);
                    //获取之前的天数:当前天数减去得到的数;
                    //获取之后的天数:去掉-;
                    string time = DateTime.Today.Add(new TimeSpan(-dayCount, 0, 0, 0, 0)).ToString("yyyy-MM-dd");
                    sWelCalse += " and NTF_A2_9995 ='" + time + "'";
                }
                if (typeCount == "5")
                {
                    string[] dayTime = oDoc.GetElementsByTagName("Time")[0].InnerText.Split('~');

                    sWelCalse += " and NTF_A2_9995 >='" + dayTime[0] + "' and NTF_A2_9995 <='" + dayTime[1] + "'";
                }
            }
            return sWelCalse;
        }
        #endregion

        #region 获取打印页面通知信息
        /// <summary>
        /// 获取打印页面通知信息
        /// </summary>
        /// <param name="NotifyId"></param>
        /// <returns></returns>
        public MsNotifyMessage GetReplyView(string NotifyId)
        {
            string cmdText = "select NTF_A2_10,NTF_A2_20,NTF_A1_20,bas_a1_40, deptname ,NTF_A2_40" +
                            " ,username,NTF_A2_9995 from NTF_A2 left join bas_a1 on bas_a1_30 = NTF_A2_100 " +
                            " left join v_dept on deptID = NTF_A2_50  left join v_user on userid = NTF_A2_9990"+
                            " left join NTF_A1 on NTF_A1_10 = NTF_A2_30  where NTF_A2_10="+NotifyId;
            MsNotifyMessage ms = null;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    ms = new MsNotifyMessage(iReader["NTF_A2_10"].ToString(), iReader["NTF_A2_20"].ToString(), iReader["NTF_A1_20"].ToString()
                                            , iReader["NTF_A2_40"].ToString(), iReader["deptname"].ToString(), null, null
                                            , null, null, null,
                                            iReader["bas_a1_40"].ToString(), iReader["NTF_A2_9995"].ToString(), null, null, iReader["username"].ToString(), null, null);
                }
            }
            return ms;
        }

        #endregion

        #region 操作浏览信息表
        /// <summary>
        /// 操作浏览信息表
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public string DoOperateReplyView(MsViewClass ms)
        {
            string cmdText="";
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string curYear = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            if (ms.state == "")   
            {               
                cmdText = "insert into NTF_A3(NTF_A3_10,NTF_A3_20,NTF_A3_30,NTF_A3_40,NTF_A3_9990,NTF_A3_9991,NTF_A3_9992,NTF_A3_9993,NTF_A3_9994,NTF_A3_9995)"+
                    " values(" + ms.NotiFyId + ",'" + ms.Userid + "','" + ms.Content.Replace("'","''") + "','"+curYear+"'," +                        
                          "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
                
            }
            else
            {
                cmdText = "update NTF_A3 set NTF_A3_30='" + ms.Content.Replace("'", "''") + "', NTF_A3_40='" + curYear + "' "+
                           " where NTF_A3_10=" + ms.NotiFyId + " and NTF_A3_20="+loginUser.UserId+"";
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if(iNum>0)
            {
                return ms.NotiFyId;
            }
            else
            {
                return "-999";
            }
        }

        #endregion
    }
}
